package com.aqgj.User.dao;

import com.aqgj.Resource.bean.Resource;
import com.aqgj.User.bean.User;
import com.aqgj.User.bean.UserVo;
import com.aqgj.common.DaoUtil;
import com.aqgj.util.BaseDaoImpl;
import com.aqgj.util.Pager;
import org.hibernate.query.NativeQuery;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.*;

/**
 * 用户数据库操作.
 */
@Repository("UserDao")
public class UserDao extends BaseDaoImpl<User> {

    public List<Resource> selectResourceListByRoleIdList(List<String> roleIdList) {
        StringBuffer sql = new StringBuffer("SELECT DISTINCT s.* " +
                "FROM role r LEFT JOIN role_resource e ON r.id = e.role_id LEFT JOIN resource s ON e.resource_id = s.id");
        sql.append(" where r.id IN (:roleIdList)");
        sql.append(" and s.resource_type = 0 order by sort asc");
        return super.currentSession().createNativeQuery(sql.toString(), Resource.class).setParameterList("roleIdList", roleIdList).getResultList();
    }


    public List<Resource> selectResourceListByRoleIdList2(List<String> roleIdList) {
        StringBuffer sql = new StringBuffer("SELECT DISTINCT s.* " +
                "FROM role r LEFT JOIN role_resource e ON r.id = e.role_id LEFT JOIN resource s ON e.resource_id = s.id");
        sql.append(" where r.id IN (:roleIdList)");
        sql.append(" and s.resource_type in (0,1) order by sort asc");
        return super.currentSession().createNativeQuery(sql.toString(), Resource.class).setParameterList("roleIdList", roleIdList).getResultList();
    }

    public User selectUserById(String id) {
        return super.get(id);
    }

    public int updateUser(User user) {
        int count = 0;
        try {
            Map<String, String> linkedHashMapMap = DaoUtil.updateAssembling(user);
            String sql = linkedHashMapMap.get("sql") + " where id = ?";
            NativeQuery nativeQuery = super.currentSession().createNativeQuery(sql);
            Iterator<Map.Entry<String, String>> iterator = linkedHashMapMap.entrySet().iterator();
            int i = 1;
            while (iterator.hasNext()) {
                Map.Entry entry = iterator.next();
                if (!entry.getKey().equals("sql")) {
                    nativeQuery.setParameter(i, entry.getValue());
                    i++;
                }
            }
            nativeQuery.setParameter(i, user.getId());
            count = nativeQuery.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException();
        }
        return count;
    }

    public Pager<User> selectDataGrid(UserVo userVo, Integer pageSize, Integer pageNumber){
        Pager<User> pager = new Pager<>();
        pager.setCurrentPage(pageNumber);
        pager.setPageSize(pageSize);
        String sql ="select * from sys_user limit ?,?";
        String countSql="select count(id) from  sys_user ";
        pager.setContent(this.getHibernateTemplate().getSessionFactory().getCurrentSession().createNativeQuery(sql,User.class).setParameter(1,pager.getStart()).setParameter(2,pageSize).getResultList());
        pager.setRecordTotal(((BigInteger)this.createSQLQuery(countSql).getResultList().get(0)).intValue());
        return pager;
    }

    public void update(User user) {
        this.getHibernateTemplate().merge(user);
    }
}